import { repayList, repaymentRepay, recharge, updateRule, getRepayDesc, sortChange } from '../api/deptmanage';
import { message } from 'antd/lib/index';
import checkStatus from '@/utils/status';

export default {
  namespace: 'repayModel',

  state: {
    data: {
      list: [],
      pagination: {},
    },
  },

  effects: {
    * fetch({ payload }, { call, put }) {
      const response = yield call(repayList, payload);
      yield checkStatus(response);
      yield put({
        type: 'getList',
        payload: response.msg,
      });
    },
    * recharge({ payload, callback }, { call, put }) {
      const response = yield call(recharge, payload);
      yield checkStatus(response);
      if (response.code === 200) {
        message.success('操作成功');
      } else {
        message.error(response.errMsg);
      }
      if (callback) callback(response);
    },


    * getRepayDesc({ payload, callback }, { call, put }) {
      const response = yield call(getRepayDesc, payload);
      yield checkStatus(response);

      yield put({
        type: 'getDesc',
        payload: response.msg,
      });
      if (callback) callback();
    },
    * repayment({ payload, callback }, { call, put }) {
      const response = yield call(repaymentRepay, payload);
      yield checkStatus(response);
      if (callback) callback(response);
    },

  },

  reducers: {
    getList(state, action) {
      state.pagination = {
        pageSize: action.payload.pageSize,
        current: action.payload.pageNo,
        total: action.payload.total,
      };
      return {
        ...state,
        data: action.payload,
      };
    },
    topUp(state, action) {
      if (action.payload.code === 200) {
        message.success('操作成功');
      } else {
        message.error(action.payload.errMsg);
      }
      return '';
    },
    edit(state, action) {
      if (action.payload.code === 200) {
        message.success('更新成功');
      } else {
        message.error(action.payload.errMsg);
      }
      return {
        ...state,
        data: action.payload,
      };
    },

    userList(state, action) {
      return {
        ...state,
        userList: action.payload.data,
      };
    },
    getDesc(state, action) {
      return {
        ...state,
        repayDesc: action.payload,
      };
    },
    deptRemove(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    uploadLists(state, { payload }) {
      return {
        ...state,
        uploadLists: payload,
      };
    },
  },
};
